package com.tinycloud.miracll.product.service.impl;


import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.StringUtils;
import com.tinycloud.common.datasource.annotation.Slave1;
import com.tinycloud.common.datasource.annotation.Slave3;
import com.tinycloud.common.datasource.annotation.Slave4;
import com.tinycloud.miracll.product.domain.ProductGather;
import com.tinycloud.miracll.product.domain.ProductInfo;
import com.tinycloud.miracll.product.mapper.ProductGatherMapper;
import com.tinycloud.miracll.product.service.IProductGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产订单生产数据采集主Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-01
 */
@Service
@Slave3
public class ProductGatherServiceImpl implements IProductGatherService
{
    @Autowired
    private ProductGatherMapper productGatherMapper;

    /**
     * 查询生产订单生产数据采集主
     * 
     * @param id 生产订单生产数据采集主ID
     * @return 生产订单生产数据采集主
     */
    @Override
    public ProductGather selectProductGatherById(String id)
    {
        return productGatherMapper.selectProductGatherById(id);
    }

    /**
     * 查询生产订单生产数据采集主列表
     * 
     * @param productGather 生产订单生产数据采集主
     * @return 生产订单生产数据采集主
     */
    @Override
    public List<ProductGather> selectProductGatherList(ProductGather productGather)
    {
        return productGatherMapper.selectProductGatherList(productGather);
    }

    /**
     * 新增生产订单生产数据采集主
     * 
     * @param productGather 生产订单生产数据采集主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductGather(ProductGather productGather)
    {
        productGather.setCreateTime(DateUtils.getNowDate());
        int rows = productGatherMapper.insertProductGather(productGather);
        insertProductInfo(productGather);
        return rows;
    }

    /**
     * 修改生产订单生产数据采集主
     * 
     * @param productGather 生产订单生产数据采集主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductGather(ProductGather productGather)
    {
        productGatherMapper.deleteProductInfoByProductorderid(productGather.getId());
        insertProductInfo(productGather);
        return productGatherMapper.updateProductGather(productGather);
    }

    /**
     * 批量删除生产订单生产数据采集主
     * 
     * @param ids 需要删除的生产订单生产数据采集主ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductGatherByIds(String[] ids)
    {
        productGatherMapper.deleteProductInfoByProductorderids(ids);
        return productGatherMapper.deleteProductGatherByIds(ids);
    }

    /**
     * 删除生产订单生产数据采集主信息
     * 
     * @param id 生产订单生产数据采集主ID
     * @return 结果
     */
    @Override
    public int deleteProductGatherById(String id)
    {
        productGatherMapper.deleteProductInfoByProductorderid(id);
        return productGatherMapper.deleteProductGatherById(id);
    }

    /**
     * 新增生产订单生产数据信息
     * 
     * @param productGather 生产订单生产数据采集主对象
     */
    public void insertProductInfo(ProductGather productGather)
    {
        List<ProductInfo> productInfoList = productGather.getProductInfoList();
        String id = productGather.getId();
        if (StringUtils.isNotNull(productInfoList))
        {
            List<ProductInfo> list = new ArrayList<ProductInfo>();
            for (ProductInfo productInfo : productInfoList)
            {
                productInfo.setProductorderID(id);
                list.add(productInfo);
            }
            if (list.size() > 0)
            {
                productGatherMapper.batchProductInfo(list);
            }
        }
    }
}
